//
//  ZLWRegisterController.m
//  EPA
//
//  Created by lanou3g on 16/4/25.
//  Copyright © 2016年 zlw. All rights reserved.
//

#import "ZLWRegisterController.h"

@interface ZLWRegisterController ()
@property (weak, nonatomic) IBOutlet UITextField *UnameLable;

@property (weak, nonatomic) IBOutlet UITextField *EmailLable;
@property (weak, nonatomic) IBOutlet UITextField *PassWordLable;
@end

@implementation ZLWRegisterController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}
- (IBAction)RegisterAction:(UIButton *)sender {
    NSURL *url = [NSURL URLWithString:@"http://api2.pianke.me/user/reg"];
    NSMutableURLRequest *requst = [[NSMutableURLRequest alloc]initWithURL:url];
    //设置 post
    [requst setHTTPMethod:@"POST"];
    //请求体
    NSString * bodyStr = [NSString stringWithFormat:@"email=%@&gender=0&uname=%@&passwd=%@",self.EmailLable.text,self.UnameLable.text,self.PassWordLable.text];
    NSData *bodyData = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];
    [requst setHTTPBody:bodyData];
    
    //使用 URLSeesion 实行网络请求
    NSURLSessionDataTask *dataTask = [[NSURLSession sharedSession] dataTaskWithRequest:requst completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        //        NSLog(@"%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
        if (data) {
            NSDictionary *jsonDcit = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
            NSDictionary *dataDict = jsonDcit[@"data"];
            UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message: dataDict[@"msg"]?dataDict[@"msg"]:@"成功" preferredStyle:(UIAlertControllerStyleAlert)];
            [alertVC addAction:[UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:nil]];
            //回到主线程
            dispatch_async(dispatch_get_main_queue(), ^{
                [self presentViewController:alertVC animated:YES completion:nil];
            });
            
        }
    }];
    [dataTask resume];

    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
